﻿#region Using statement

using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using WAS.Logic.SampleLevel;
using Utilities.Menu.UIElements;
using WAS.Menu;
using Utilities.Menu;
using System.Collections.ObjectModel;
using WAS.Resources;
using Utilities;

#endregion

namespace WAS
{

  /// <summary>
  /// Le costanti di gioco, i path della content pipeline, e in generale le cose
  /// che possiamo voler cambiare durante lo sviluppo (e che sono usate in piú
  /// punti del codice) stanno nel ConfigurationService
  /// </summary>
  public class ConfigurationService
  {
    public static float level_up_val = 0.16f;

    public string LaserModel { get; set; }
    public string NukeModel { get; set; }
    public string SwarmModel { get; set; }
    public string AsteroidModel { get; set; }
    public ArrayList<ShipDescription> Ships { get; set; }

    public readonly int NUM_SHIPS_MODELS = 18;
    public ShipDescription this[ShipType ship_type]
    {
      get
      {
        int to_return = (int)(ship_type);
        if (to_return >= 0)
          return Ships[to_return];
        else return new ShipDescription();
      }
    }

    #region Base Configuration

    public static ConfigurationService GetBaseConfiguration()
    {
      return new ConfigurationService()
          {
            LaserModel = @"Projectiles\Laser\model",
            NukeModel = @"Projectiles\cruise_nuke",
            SwarmModel = @"Projectiles\cruise",
            AsteroidModel = @"Asteroid\model",
            Ships = new ArrayList<ShipDescription>(
              new[]{
                  //Boss1 -> 17
                   new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.Boss1,
                      ModelPath = @"Capital Ships\Bosses\boss6",
                      Speed = 0.3f,
                      PrimaryWeapons= new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Chain_Reaction), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Shockwave) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 1.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Green,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //Boss1 -> 16
                   new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.Boss1,
                      ModelPath = @"Capital Ships\Bosses\boss5",
                      Speed = 0.3f,
                      PrimaryWeapons= new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Chain_Reaction), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Shockwave) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 1.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Green,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //Boss1 -> 15
                   new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.Boss1,
                      ModelPath = @"Capital Ships\Bosses\boss4",
                      Speed = 0.3f,
                      PrimaryWeapons= new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Chain_Reaction), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Shockwave) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 1.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Green,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //Boss1 -> 14
                   new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.Boss1,
                      ModelPath = @"Capital Ships\Bosses\boss3",
                      Speed = 0.3f,
                      PrimaryWeapons= new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Chain_Reaction), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Shockwave) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 1.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Green,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                   //Boss1 -> 13
                   new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.Boss1,
                      ModelPath = @"Capital Ships\Bosses\boss2",
                      Speed = 0.3f,
                      PrimaryWeapons= new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Chain_Reaction), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Shockwave) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 1.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Green,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //Boss1 -> 12
                   new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.Boss1,
                      ModelPath = @"Capital Ships\Bosses\boss1",
                      Speed = 0.3f,
                      PrimaryWeapons= new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Chain_Reaction), WeaponConfig.GetWeapon(ShipType.Boss1, WeaponType.Shockwave) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 1.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Green,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //EnemyBattleCruiser -> 11
                  new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.EnemyBattleCruiser,
                      ModelPath = @"Capital Ships\e_sf09",
                      Speed = 0.4f,
                      PrimaryWeapons= new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Triple_Plasma)},
                      SecondaryWeapons = new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.EnemyBattleCruiser, WeaponType.Lightning) },
                      PrimaryShotSpawnTime = 0.6f,
                      SecondaryShotSpawnTime = 6,
                      EnergyGeneratorLevel = 2,
                      Armor = 0.6f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.4f,
                          initialColor=Color.Yellow,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                    //EnemyCruiser -> 10
                  new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.EnemyCruiser,
                      ModelPath = @"Capital Ships\e_sf05",
                      Speed = 0.4f,
                      PrimaryWeapons= new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Triple_Plasma)},
                      SecondaryWeapons = new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.EnemyCruiser, WeaponType.Lightning) },
                      PrimaryShotSpawnTime = 0.6f,
                      SecondaryShotSpawnTime = 6,
                      EnergyGeneratorLevel = 2,
                      Armor = 0.6f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.4f,
                          initialColor=Color.Yellow,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //EnemyFrigate -> 9
                  new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.EnemyFrigate,
                      ModelPath = @"Capital Ships\e_sf04",
                      Speed = 0.2f,
                      PrimaryWeapons= new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Double_Plasma), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Triple_Plasma), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Laser)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Smart_Missiles), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Lightning), WeaponConfig.GetWeapon(ShipType.EnemyFrigate, WeaponType.Chain_Reaction) },
                      PrimaryShotSpawnTime = 0.7f,
                      SecondaryShotSpawnTime = 7,
                      EnergyGeneratorLevel = 3,
                      Armor = 0.8f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.5f,
                          initialColor=Color.Wheat,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //EnemyCorvette -> 8
                  new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.EnemyCorvette,
                      ModelPath = @"Capital Ships\e_sf07",
                      Speed = 0.6f,
                      PrimaryWeapons= new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyCorvette, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.EnemyCorvette, WeaponType.Plasma), WeaponConfig.GetWeapon(ShipType.EnemyCorvette, WeaponType.Double_Plasma) },
                      SecondaryWeapons = new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyCorvette, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.EnemyCorvette, WeaponType.Missiles_Swarm), WeaponConfig.GetWeapon(ShipType.EnemyCorvette, WeaponType.Smart_Missiles) },
                      PrimaryShotSpawnTime = 0.5f,
                      SecondaryShotSpawnTime = 5,
                      EnergyGeneratorLevel = 2,
                      Armor = 0.6f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.3f,
                          initialColor=Color.Purple,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //EnemyDestroyer -> 7
                  new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.EnemyDestroyer,
                      ModelPath = @"Capital Ships\e_sf06",
                      Speed = 0.8f,
                      PrimaryWeapons= new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyDestroyer, WeaponType.Machine_Gun), WeaponConfig.GetWeapon(ShipType.EnemyDestroyer, WeaponType.Plasma) },
                      SecondaryWeapons = new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyDestroyer, WeaponType.Nuke), WeaponConfig.GetWeapon(ShipType.EnemyDestroyer, WeaponType.Missiles_Swarm) },
                      PrimaryShotSpawnTime = 0.4f,
                      SecondaryShotSpawnTime = 4,
                      EnergyGeneratorLevel = 1,
                      Armor = 0.2f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=0.2f,
                          initialColor=Color.Blue,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  //EnemyFighter -> 6
                  new ShipDescription()
                  {
                      MaxConcurrentTargets = 1,
                      ShipType = WAS.ShipType.EnemyFighter,
                      ModelPath = @"Capital Ships\e_sf10",
                      Speed = 1.0f,
                      PrimaryWeapons= new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyFighter, WeaponType.Machine_Gun)  },
                      SecondaryWeapons = new ArrayList<WeaponConfig>(5) { WeaponConfig.GetWeapon(ShipType.EnemyFighter, WeaponType.Nuke) },
                      PrimaryShotSpawnTime = 0.3f,
                      SecondaryShotSpawnTime = 3,
                      EnergyGeneratorLevel = 1,
                      Armor = 0.0f,
                      TimeToWarp = 1.0f,
                      ShockWaveOnExplode = true,
                      ShockWaveData = new ShockWaveData
                      {
                          damage=1.0f,
                          initialColor=Color.Red,
                          finalColor=Color.Gray,
                          speed=8f
                      }
                  },
                  // PlayerCruiser -> 5
                       new ShipDescription()
                      {
                        MaxConcurrentTargets = 3,
                        ShipType = WAS.ShipType.PlayerCruiser,
                        ModelPath = @"Capital Ships\p_sf12",
                        Speed = 0.7f,
                        PrimaryWeapons= new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerCruiser, WeaponType.Machine_Gun) },
                        SecondaryWeapons = new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerCruiser, WeaponType.Nuke) },
                        Armor = 0.7f,
                        TimeToWarp = 0.7f,
                        MaxShield = 7.0f,
                        EnergyGeneratorLevel = 1,
                        PrimaryShotSpawnTime = 0.0f,
                        SecondaryShotSpawnTime = 0.0f,
                        ShockWaveOnExplode=false
                      },
                      // PlayerFrigate -> 4
                      new ShipDescription()
                      {
                        MaxConcurrentTargets = 3,
                        ShipType = WAS.ShipType.PlayerFrigate,
                        ModelPath = @"Capital Ships\p_sf13",
                        Speed = 0.55f,
                        PrimaryWeapons= new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerFrigate, WeaponType.Machine_Gun)},
                        SecondaryWeapons = new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerFrigate, WeaponType.Nuke)},
                        Armor = 0.6f,
                        TimeToWarp = 0.5f,
                        MaxShield = 6.0f,
                        EnergyGeneratorLevel = 1,
                        PrimaryShotSpawnTime = 0.0f,
                        SecondaryShotSpawnTime = 0.0f,
                        ShockWaveOnExplode=false
                      },
                      // PlayerCorvette -> 3
                      new ShipDescription()
                      {
                        MaxConcurrentTargets = 2,
                        ShipType = WAS.ShipType.PlayerCorvette,
                        ModelPath = @"Capital Ships\p_sf08",
                        Speed = 0.7f,
                        PrimaryWeapons= new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerCorvette, WeaponType.Machine_Gun) },
                        SecondaryWeapons = new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerCorvette, WeaponType.Nuke) },
                        Armor = 0.5f,
                        TimeToWarp = 0.6f,
                        MaxShield = 5.0f,
                        EnergyGeneratorLevel = 1,
                        PrimaryShotSpawnTime = 0.0f,
                        SecondaryShotSpawnTime = 0.0f,
                        ShockWaveOnExplode=false
                        
                      },
                      //Player Destroyer -> 2
                      new ShipDescription()
                      {
                        MaxConcurrentTargets = 2,
                        ShipType = WAS.ShipType.PlayerDestroyer,
                        ModelPath = @"Capital Ships\p_sf01",
                        Speed = 0.2f,
                        PrimaryWeapons= new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerDestroyer, WeaponType.Machine_Gun) },
                        SecondaryWeapons = new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerDestroyer, WeaponType.Nuke) },
                        Armor = 0.7f,
                        TimeToWarp = 0.25f,
                        MaxShield = 4.0f,
                        EnergyGeneratorLevel = 1,
                        PrimaryShotSpawnTime = 0.0f,
                        SecondaryShotSpawnTime = 0.0f,
                        ShockWaveOnExplode=true
                      },
                      // //Player Fighter -> 1
                      new ShipDescription()
                      {
                        MaxConcurrentTargets = 1,
                        ShipType = WAS.ShipType.PlayerFighter,
                        ModelPath = @"Capital Ships\p_sf02",
                        Speed = 0.3f,                        
                        PrimaryWeapons= new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerFighter, WeaponType.Machine_Gun) },
                        SecondaryWeapons = new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerFighter, WeaponType.Nuke) },
                        Armor = 0.4f,
                        TimeToWarp = 0.35f,
                        MaxShield = 3.0f,
                        EnergyGeneratorLevel = 1,
                        PrimaryShotSpawnTime = 0.0f,
                        SecondaryShotSpawnTime = 0.0f,
                        ShockWaveOnExplode=true
                      },
                    // PlayerBase - >0
                    new ShipDescription()
                    {
                        MaxConcurrentTargets = 1,
                        ShipType = WAS.ShipType.PlayerBaseShip,
                        ModelPath = @"Capital Ships\p_sf03",
                        Speed = 0.15f,
                        PrimaryWeapons= new ArrayList<WeaponConfig> { 
                          WeaponConfig.GetWeapon(ShipType.PlayerBaseShip, WeaponType.Machine_Gun) },
                        SecondaryWeapons = new ArrayList<WeaponConfig> { WeaponConfig.GetWeapon(ShipType.PlayerBaseShip, WeaponType.Nuke) },
                        Armor = 0.15f,
                        TimeToWarp = 0.15f,
                        MaxShield = 2.0f,
                        EnergyGeneratorLevel = 1,
                        PrimaryShotSpawnTime = 0.0f,
                        SecondaryShotSpawnTime = 0.0f,
                        ShockWaveOnExplode=false
                    },
                  }.Reverse().ToList())
          };

      #region Vecchi Dati
      //  new ShipDescription()
      //  {
      //      ModelPath = @"Capital Ships\Bosses\sf22",
      //        Speed = 0.4f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 10.0f,    
      //            Damage = 1.0f,
      //            Type = WeaponType.TriplePlasma,
      //            ShootsNumber = 3,
      //            FireRate = 0.8f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 10.0f,
      //            Damage = 1.0f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20   
      //        },
      //        LaserShotSpawnTime = 0.2f,
      //        NukeShotSpawnTime = 5,
      //        available_nukes = 1000,
      //        Armor = 1.0f,
      //        TimeToWarp = 1.0f
      //  },
      //   //EnemyFighter -> 10
      //  new ShipDescription()
      //  {
      //      ModelPath = @"Capital Ships\e_sf10",
      //        Speed = 0.75f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 8.0f,    
      //            Damage = 0.1f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 3,
      //            FireRate = 0.3f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,
      //            Damage = 0.25f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //        LaserShotSpawnTime = 0.5f,
      //        NukeShotSpawnTime = 10,
      //        available_nukes = 3,
      //        Armor = 1.0f,
      //        TimeToWarp = 1.0f
      //  },
      //  //EnemyDestroyer -> 9
      //  new ShipDescription()
      //  {
      //      ModelPath = @"Capital Ships\e_sf06",
      //      Speed = 0.2f,
      //      PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 5.0f,    
      //            Damage = 0.2f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 3,
      //            FireRate = 0.3f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 10.0f,
      //            Damage = 0.7f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //      LaserShotSpawnTime = 0.4f,
      //      NukeShotSpawnTime = 4,
      //      available_nukes = 10,
      //      Armor = 1.0f,
      //      TimeToWarp = 1.0f
      //  },
      //    //EnemyCorvette -> 8
      //  new ShipDescription()
      //  {
      //      ModelPath = @"Capital Ships\e_sf07",
      //      Speed = 0.4f,
      //      PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 6.0f,    
      //            Damage = 0.3f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 3,
      //            FireRate = 0.3f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 8.0f,
      //            Damage = 0.6f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //      LaserShotSpawnTime = 0.5f,
      //      NukeShotSpawnTime = 8,
      //      available_nukes = 5,
      //      Armor = 1.0f,
      //      TimeToWarp = 1.0f
      //  },
      //    //EnemyCruiser -> 7
      //  new ShipDescription()
      //  {
      //      ModelPath = @"Capital Ships\e_sf05",
      //      Speed = 0.5f,
      //      PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,    
      //            Damage = 0.2f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 3,
      //            FireRate = 0.3f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 6.6f,
      //            Damage = 0.6f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //      LaserShotSpawnTime = 0.2f,
      //      NukeShotSpawnTime = 15,
      //      available_nukes = 4,
      //      Armor = 1.0f,
      //      TimeToWarp = 1.0f
      //  },

      //  //EnemyFrigate -> 6
      //  new ShipDescription()
      //  {
      //      ModelPath = @"Capital Ships\e_sf04",
      //      Speed = 0.6f,
      //      PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,
      //            Damage = 0.4f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 5,
      //            FireRate = 0.2f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 6.0f,
      //            Damage = 1.1f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //      LaserShotSpawnTime = 0.5f,
      //      NukeShotSpawnTime = 3,
      //      available_nukes = 4,
      //      Armor = 1.0f,
      //      TimeToWarp = 1.0f
      //  },
      //  // PlayerFrigate -> 5
      //       new ShipDescription()
      //      {
      //        ModelPath = @"Capital Ships\p_sf12",
      //        Speed = 0.7f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 4.0f,    
      //            Damage = 0.5f,
      //            Type = WeaponType.TriplePlasma,
      //            ShootsNumber = 4,
      //            FireRate = 0.2f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 5.0f,
      //            Damage = 0.5f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //        Armor = 0.5f,
      //        TimeToWarp = 0.5f,
      //        MaxShield = 7.0f,
      //        available_nukes = 2,
      //        LaserShotSpawnTime = 0.0f,
      //        NukeShotSpawnTime = 0.0f
      //      },
      //      // PlayerFighter -> 4
      //      new ShipDescription()
      //      {
      //        ModelPath = @"Capital Ships\p_sf13",
      //        Speed = 0.85f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,    
      //            Damage = 0.35f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 5,
      //            FireRate = 0.2f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 6.5f,
      //            Damage = 0.35f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //        Armor = 0.375f,
      //        TimeToWarp = 0.25f,
      //        MaxShield = 6.0f,
      //        available_nukes = 2,
      //        LaserShotSpawnTime = 0.0f,
      //        NukeShotSpawnTime = 0.0f
      //      },
      //      // PlayerCorvette -> 3
      //      new ShipDescription()
      //      {
      //        ModelPath = @"Capital Ships\p_sf08",
      //        Speed = 0.9f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,    
      //            Damage = 0.15f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 3,
      //            FireRate = 0.1f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,
      //            Damage = 0.15f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //        Armor = 0.2f,
      //        TimeToWarp = 0.125f,
      //        MaxShield = 5.0f,
      //        available_nukes = 2,
      //        LaserShotSpawnTime = 0.0f,
      //        NukeShotSpawnTime = 0.0f
      //      },
      //      //Player Destroyer -> 2
      //      new ShipDescription()
      //      {
      //        ModelPath = @"Capital Ships\p_sf01",
      //        Speed = 0.4f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,    
      //            Damage = 0.15f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 5,
      //            FireRate = 0.3f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 7.0f,
      //            Damage = 0.15f,
      //            Type = WeaponType.TrackingNuke,
      //            Mass = 1f,
      //            Life = 10,
      //            Range = 20      
      //        },
      //        Armor = 0.35f,
      //        TimeToWarp = 0.125f,
      //        MaxShield = 4.0f,
      //        available_nukes = 2,
      //        LaserShotSpawnTime = 0.0f,
      //        NukeShotSpawnTime = 0.0f
      //      },
      //      // //Player Destroyer -> 1
      //      new ShipDescription()
      //      {
      //        ModelPath = @"Capital Ships\p_sf02",
      //        Speed = 0.25f,
      //        PrimaryWeapon = new WeaponConfig
      //        {
      //            Speed = 5.0f,    
      //            Damage = 0.175f,
      //            Type = WeaponType.Plasma,
      //            ShootsNumber = 5,
      //            FireRate = 0.4f,
      //            Mass = 0.5f
      //        },
      //        SecondaryWeapon = new WeaponConfig
      //        {
      //            Speed = 5.0f,
      //            Damage = 0.03f,
      //            Type = WeaponType.MissilesSwarm,
      //            Mass = 0.4f,
      //            Life = 3,
      //            Range = 20,
      //            NumConcurrentShots = 5
      //        },
      //        Armor = 0.45f,
      //        TimeToWarp = 0.25f,
      //        MaxShield = 3.0f,
      //        available_nukes = 2,
      //        LaserShotSpawnTime = 0.0f,
      //        NukeShotSpawnTime = 0.0f
      //      },
      //    // PlayerBase - >0
      //    new ShipDescription()
      //    {
      //    ModelPath = @"Capital Ships\p_sf03",
      //    Speed = 0.7f,
      //    PrimaryWeapon = new WeaponConfig
      //    {
      //        Speed = 25.0f,    
      //        Damage = 0.05f,
      //        Type = WeaponType.Laser,
      //        ShootsNumber = 12,
      //        FireRate = 0.25f,
      //        Mass = 0.5f,
      //        Range = 20
      //    },
      //    SecondaryWeapon = new WeaponConfig
      //    {
      //        Speed = 25.0f,
      //        Damage = 0.1f,
      //        Type = WeaponType.Lightning,
      //        NumConcurrentShots = 5,
      //        Mass = 0.0001f,
      //        Life = 100,
      //        Range = 15                     
      //    },
      //    Armor = 0.025f,
      //    TimeToWarp = 0.025f,
      //    MaxShield = 2.0f,
      //    available_nukes = 2,
      //    LaserShotSpawnTime = 0.0f,
      //    NukeShotSpawnTime = 0.0f
      //    },
      //  }.Reverse().ToList())
      //};
      #endregion
    }

    #endregion

    #region Default Configuration

    //public static ConfigurationService GetDefaulConfiguration()
    //{
    //  var state = Leaderboard.GetCampaignState();
    //  var config = GetBaseConfiguration();

    //  for (int i = 0; i <= (int)ShipType.PlayerCruiser; i++)
    //  {
    //    var s = config.Ships[i];

    //    var luv = (level_up_val / ((float)ShipType.PlayerCruiser + 1.0f)) * (i + 1);

    //    s.MaxShield = (int)(s.MaxShield + (float)Math.Floor(luv * 10 * state.Ships[i].ShLevel));
    //    s.Speed = Math.Min(s.Speed + luv * state.Ships[i].EnLevel, 1.0f);
    //    s.Armor = Math.Min(s.Armor + luv * state.Ships[i].ArLevel, 1.0f);
    //    s.EnergyGeneratorLevel = state.Ships[i].EnLevel + 1;

    //    s.PrimaryWeapons.Clear();
    //    s.SecondaryWeapons.Clear();

    //    for (int j = 0; j < state.Ships[i].Weapons.Count; j++)
    //      if (state.Ships[i].Weapons[j].UpgradeLv >= 0)
    //      {
    //        var t = (WeaponType)state.Ships[i].Weapons[j].Type;

    //        var w = WeaponConfig.GetWeapon((ShipType)state.Ships[i].Index, t);
    //        w.Damage = Math.Min(w.Damage + luv * state.Ships[i].Weapons[j].UpgradeLv, 1.0f);
    //        if (IsPrimary(t))
    //          s.PrimaryWeapons.Add(w);
    //        else
    //          s.SecondaryWeapons.Add(w);

    //      }

    //    s.TimeToWarp = Math.Min(s.TimeToWarp + luv * state.Ships[i].FTLevel, 1.0f);

    //    config.Ships[i] = s;
    //  }

    //  return config;
    //}

    #endregion

    #region Saved Configuration

    public static float MaxShield = 7.0f * 1.3f;
    public static ConfigurationService GetDefaulConfiguration()
    {
      CampaignState state = Leaderboard.GetCampaignState().CampaignState;
      var config = GetBaseConfiguration();

      for (int i = 0; i <= (int)ShipType.PlayerCruiser; i++)
      {
        var s = config.Ships[i];

        var luv = 0.1f;

        s.MaxShield = (int)(s.MaxShield + (float)Math.Floor(s.MaxShield * luv * state.Ships[i].ShLevel));
        s.Speed = s.Speed + s.Speed * luv * state.Ships[i].EnLevel;
        s.Armor = s.Armor + s.Armor * luv * state.Ships[i].ArLevel;
        s.EnergyGeneratorLevel = state.Ships[i].EnLevel + 1;

        s.PrimaryWeapons.Clear();
        s.SecondaryWeapons.Clear();

        for (int j = 0; j < state.Ships[i].Weapons.Count; j++)
          if (state.Ships[i].Weapons[j].UpgradeLv >= 0)
          {
            var t = (WeaponType)state.Ships[i].Weapons[j].Type;
            var w = WeaponConfig.GetWeapon((ShipType)state.Ships[i].Index, t);
            w.Damage = w.Damage + w.Damage * luv * state.Ships[i].Weapons[j].UpgradeLv;
            if (IsPrimary(t))
              s.PrimaryWeapons.Add(w);
            else
              s.SecondaryWeapons.Add(w);
          }

        s.TimeToWarp = s.TimeToWarp + s.TimeToWarp * luv * state.Ships[i].FTLevel;

        config.Ships[i] = s;
      }

      return config;
    }

    #endregion

    public static void ChangeConfiguration(Game game, ConfigurationService config)
    {
      game.Services.RemoveService(typeof(ConfigurationService));
      game.Services.AddService(typeof(ConfigurationService), config);
    }

    public static bool IsPrimary(WeaponType type)
    {
      switch (type)
      {
        case WeaponType.Machine_Gun:
        case WeaponType.Plasma:
        case WeaponType.Double_Plasma:
        case WeaponType.Triple_Plasma:
        case WeaponType.Laser:
          return true;
        default:
          return false;
      }
    }

  }
}
